package com.app.datasource;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

import javax.sql.DataSource;

/**
 * @author ch
 * @date 2022/12/14 10:54
 */
public class HikarDatasourceFactory extends AbstractDatabaseFactory {

    @Override
    public DataSource init(DatasourceInfo info) {
        try {
            HikariConfig hikariConfig = new HikariConfig();
            hikariConfig.setConnectionTimeout(10000L);
            hikariConfig.setRegisterMbeans(true);
            hikariConfig.setMaxLifetime(60000L);
            hikariConfig.setMaximumPoolSize(10);
            hikariConfig.setJdbcUrl(info.getUrl());
            hikariConfig.setDriverClassName(info.getDriverClassName());
            hikariConfig.setUsername(info.getUsername());
            hikariConfig.setPassword(info.getPassword());
            hikariConfig.addDataSourceProperty("cachePreStmts", "true");
            hikariConfig.addDataSourceProperty("preStmtCacheSize", "258");
            hikariConfig.addDataSourceProperty("preStmtCacheSqlLimit", "2048");
            hikariConfig.setPoolName("HikariPool-" + info.getId());
            hikariConfig.setMinimumIdle(10);
            hikariConfig.setMaximumPoolSize(10);

            HikariDataSource hikariDataSource = new HikariDataSource(hikariConfig);
            return hikariDataSource;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
//        return DataSourceBuilder.create().url(info.getUrl()).driverClassName(info.getDriverClassName()).username(info.getUsername()).password(info.getPassword()).build();
    }
}
